package com.ql.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ql.dao.DeviceDao;
import com.ql.dao.SectionDao;
import com.ql.domain.Device;
import com.ql.domain.DeviceMonitoring;
import com.ql.domain.Section;
import com.ql.domain.request.PermissionQuery;
import com.ql.domain.response.JsonResult;
import com.ql.service.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    DeviceDao deviceDao;
    @Autowired
    SectionDao sectionDao;
    @Override
    public JsonResult findAll(PermissionQuery permissionQuery, String devNum, Integer page, Integer limit) {
        //进行分页
        List<Device> deviceArrayList = new ArrayList<>();
        //如果userId不为空并且projectId和sectionId为空
        if (StringUtils.isNotEmpty(permissionQuery.getUserId()) && StringUtils.isEmpty(permissionQuery.getProjectId()) && StringUtils.isEmpty(permissionQuery.getSectionId())) {
            //根据userId查询出对应的设备id
            List<Section> sectionList = sectionDao.findDeviceByUserId(permissionQuery.getUserId());
            PageHelper.startPage(page, limit);
            deviceArrayList = disposeList(sectionList, devNum);
        }
        //如果projectId不为空,sectionId为空
        else if (StringUtils.isNotEmpty(permissionQuery.getProjectId()) && StringUtils.isEmpty(permissionQuery.getSectionId())) {
            //根据projectId查询对应的设备id
            List<Section> sectionList = sectionDao.findByProjectId(permissionQuery.getProjectId());
            PageHelper.startPage(page, limit);
            deviceArrayList = disposeList(sectionList, devNum);
        } else if (StringUtils.isNotEmpty(permissionQuery.getSectionId())) {
            List<Section> sectionList = sectionDao.findBySectionId(permissionQuery.getSectionId());
            PageHelper.startPage(page, limit);
            deviceArrayList = disposeList(sectionList, devNum);
        }

        PageInfo pageInfo = new PageInfo(deviceArrayList);
        return JsonResult.success(pageInfo.getList(), pageInfo.getTotal());
    }


    @Override
    public JsonResult save(Device device) {
        try {
            deviceDao.save(device);
            return JsonResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("添加失败");
        }
    }

    @Override
    public JsonResult update(Device device) {
        try {
            deviceDao.update(device);
            return JsonResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public JsonResult del(String id) {
        try {
            deviceDao.del(id);
            return JsonResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public List<Device> getDeviceList(PermissionQuery permissionQuery) {
        List<Device> deviceArrayList = new ArrayList<>();
        //如果userId不为空并且projectId和sectionId为空
        if (StringUtils.isNotEmpty(permissionQuery.getUserId()) && StringUtils.isEmpty(permissionQuery.getProjectId()) && StringUtils.isEmpty(permissionQuery.getSectionId())) {
            //根据userId查询出对应的设备id
            List<Section> sectionList = sectionDao.findDeviceByUserId(permissionQuery.getUserId());
            deviceArrayList = disposeList(sectionList, null);
        }
        //如果projectId不为空,sectionId为空
        else if (StringUtils.isNotEmpty(permissionQuery.getProjectId()) && StringUtils.isEmpty(permissionQuery.getSectionId())) {
            //根据projectId查询对应的设备id
            List<Section> sectionList = sectionDao.findByProjectId(permissionQuery.getProjectId());
            deviceArrayList = disposeList(sectionList, null);
        } else if (StringUtils.isNotEmpty(permissionQuery.getSectionId())) {
            List<Section> sectionList = sectionDao.findBySectionId(permissionQuery.getSectionId());
            deviceArrayList = disposeList(sectionList, null);
        }

        return deviceArrayList;
    }


    private List<Device> disposeList(List<Section> sectionList, String devNum) {
        //保存设备id
        ArrayList<Integer> deviceIds = new ArrayList<>();
        if (sectionList != null) {
            for (Section section : sectionList) {
                List<Device> devices = section.getDevices();
                for (Device device : devices) {
                    //获得用户关联的所有设备id
                    deviceIds.add(device.getId());
                }
            }
            if (deviceIds.size()==0){
                return new ArrayList<Device>();
            }
            //处理查询结果,将进度和当前作业查询出并赋值
            return deviceDao.findAllByCondition(deviceIds, devNum);

        }
        return new ArrayList<Device>();
    }

}
